package net.mymilkedeek.ludum.tinythumb.model;

import net.mymilkedeek.ludum.tinythumb.util.Constants;
import net.mymilkedeek.ludum.tinythumb.util.URL;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Polygon;
import org.newdawn.slick.state.StateBasedGame;

/**
 * TODO: Write Documentation
 *
 * @author Michael Demey
 */
public class Player {
    private Polygon polygon;
    private float xcoord;
    private float ycoord;
    private SpriteSheet[] spriteSheetCharacter;
    private Animation animationCharacter;
    public static int direction_faced;
    private Party party;

    public Player(int x, int y) throws SlickException {
        spriteSheetCharacter = new SpriteSheet[4];
        spriteSheetCharacter[Constants.direction_right] = new SpriteSheet(URL.character_sprites_right, Constants.tile_size, Constants.tile_size);
        spriteSheetCharacter[Constants.direction_left]  = new SpriteSheet(URL.character_sprites_left, Constants.tile_size, Constants.tile_size);
        spriteSheetCharacter[Constants.direction_top]  = new SpriteSheet(URL.character_sprites_up, Constants.tile_size, Constants.tile_size);
        spriteSheetCharacter[Constants.direction_bottom]  = new SpriteSheet(URL.character_sprites_down, Constants.tile_size, Constants.tile_size);

        animationCharacter = new Animation(spriteSheetCharacter[Constants.direction_default], Constants.animation_character_refresh_speed);
        direction_faced = Constants.direction_default;

        xcoord = x ;// 2;
        ycoord = y ;// 2;

        polygon = new Polygon(new float[] {
                xcoord, ycoord,
                xcoord + Constants.tile_size, ycoord,
                xcoord + Constants.tile_size, ycoord + Constants.tile_size,
                xcoord, ycoord + Constants.tile_size
        });
        
        party = new Party();
        Member member = new Member();
        member.name = "Thumb";
        member.hp = 200;
        member.atk = 10;
        member.def = 15;
        member.lvl = 5;
        member.xptoNxt = 10;
        party.setMember1(member);
        member = new Member();
        member.name = "Chic";
        member.hp = 180;
        member.atk = 20;
        member.def = 18;
        member.lvl = 5;
        member.xptoNxt = 10;
    }

    public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
        animationCharacter.draw(Constants.center_screen_x, Constants.center_screen_y);
    }

    public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
        Input input = gameContainer.getInput();
        float movementDifference = ((float) delta) / Constants.movement_speed;

        if (input.isKeyDown(Input.KEY_LEFT) || input.isKeyDown(Input.KEY_RIGHT)) {
            if (input.isKeyDown(Input.KEY_LEFT)) {
                setAnimationCharacter(Constants.direction_left);
                direction_faced = Constants.direction_left;
                setXcoord(getXcoord() - movementDifference);
            } else {
                setAnimationCharacter(Constants.direction_right);
                direction_faced = Constants.direction_right;
                setXcoord(getXcoord() + movementDifference);
            }
        }

        if ( input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_DOWN)) {
            if ( input.isKeyDown(Input.KEY_UP)) {
                setAnimationCharacter(Constants.direction_top);
                direction_faced = Constants.direction_top;
                setYcoord(getYcoord() - movementDifference);
            } else {
                setAnimationCharacter(Constants.direction_bottom);
                direction_faced = Constants.direction_bottom;
                setYcoord(getYcoord() + movementDifference);
            }
        }
    }

    public Polygon getPolygon() {
        return polygon;
    }

    public void setPolygon(Polygon polygon) {
        this.polygon = polygon;
    }

    public float getXcoord() {
        return xcoord;
    }

    public void setXcoord(float xcoord) {
        this.xcoord = xcoord;
        polygon.setX(xcoord);
    }

    public float getYcoord() {
        return ycoord;
    }

    public void setYcoord(float ycoord) {
        this.ycoord = ycoord;
        polygon.setY(ycoord);
    }

    public Animation getAnimationCharacter() {
        return animationCharacter;
    }

    public void setAnimationCharacter(int animation) {
        this.animationCharacter = new Animation(spriteSheetCharacter[animation], Constants.animation_character_refresh_speed);
    }
}
